package indi.kider.server.woniubus.service;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.constant.NoticeCons;
import indi.kider.common.model.apartment.Community;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.TenantQuery;
import indi.kider.common.model.woniubus.NoticeVo;
import indi.kider.server.apabus.mapper.NoticeMapper;
import indi.kider.server.employee.mapper.TenantMapper;
import indi.kider.server.woniubus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.model.base.CoreException;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;
import pers.vankid.common.utils.page.PageUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
@Service
public class NoticeSericeImpl implements NoticeSerice {

    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private TenantMapper tenantMapper;



    /**
     * 上位企业管理员
     * 根据字段查询获取通知列表
     * @return
     */
    public PageResponse<List<Map>> getNoticeListForSystem(PageRequest<NoticeVo> request, Employee employee){
        NoticeVo noticeVo = request.getQuery();
        if(!employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {
            noticeVo.setEnterpriseId(employee.getEnterpriseId());
            if(!employee.getRoles().equals(EmployeeRoleCons.SUPERENTERPRISE)) {
                noticeVo.setOperatorId(employee.getId());
            }
        }
        noticeVo.setType(NoticeCons.TYPE_SYS);
        PageUtil.start(request);
        List<Map> list=  noticeMapper.getNoticeList(noticeVo);
        PageResponse<List<Map>> pageResponse = PageUtil.getPageResponse(list);
        return pageResponse;
    }

    /**
     * 上位企业管理员
     * 发送通知
     * @param noticeVo
     * @param employee
     * @return
     */
    public CommonResponse sendNoticeBySystem(NoticeVo noticeVo, Employee employee){
        noticeVo.setEnterpriseId(employee.getEnterpriseId());
        noticeVo.setOperatorId(employee.getId());
        noticeVo.setOpTime(new Date());
        noticeVo.setType(NoticeCons.TYPE_SYS);
        noticeMapper.batchInsertNotice(noticeVo);
        return CommonResponse.buildAddSuccessResponse("保存通知成功!",null);
    }


    /**
     * 合作企业员工
     * 根据字段查询获取通知列表
     * @return
     */
    public PageResponse<List<Map>> getNoticeListForPart(PageRequest<NoticeVo> request, Employee employee){
        NoticeVo noticeVo = request.getQuery();
        noticeVo.setEnterpriseId(employee.getEnterpriseId());
        noticeVo.setOperatorId(employee.getId());
        noticeVo.setType(NoticeCons.TYPE_QY);
        PageUtil.start(request);
        List<Map> list=  noticeMapper.getNoticeList(noticeVo);
        PageResponse<List<Map>> pageResponse = PageUtil.getPageResponse(list);
        return pageResponse;
    }

    /**
     * 合作企业员工
     * 发送通知
     * @param noticeVo
     * @param employee
     * @return
     */
    public CommonResponse sendNoticeByPart(NoticeVo noticeVo, Employee employee)throws Exception{
        //设置房源信息
        TenantQuery tenantQuery = new TenantQuery();
        tenantQuery.setEmployeePartId(employee.getId());
        List<Map> list = tenantMapper.getPartHouseList(tenantQuery);
        if(list.size()==0){
            throw new CoreException("尚未有分配的房源信息,无法发送通知!");
        }
        Long[] temp = new Long[list.size()];
        int i= 0;
        for(Map map : list){
            temp[i++] = Long.valueOf(map.get("id").toString());
        }
        noticeVo.setHouseIds(temp);
        //设置参数
        noticeVo.setEnterpriseId(employee.getEnterpriseId());
        noticeVo.setOperatorId(employee.getId());
        noticeVo.setOpTime(new Date());
        noticeVo.setType(NoticeCons.TYPE_QY);

        noticeMapper.batchInsertNotice(noticeVo);
        return CommonResponse.buildAddSuccessResponse("保存通知成功!",null);
    }

    /**
     * 合作企业员工根据房源
     * 发送通知
     * @param noticeVo
     * @param employee
     * @return
     */
    public CommonResponse sendNoticeByPartByHouseList(NoticeVo noticeVo, Employee employee)throws Exception{
        //设置参数
        noticeVo.setEnterpriseId(employee.getEnterpriseId());
        noticeVo.setOperatorId(employee.getId());
        noticeVo.setOpTime(new Date());
        noticeVo.setType(NoticeCons.TYPE_QY);
        noticeMapper.batchInsertNotice(noticeVo);
        return CommonResponse.buildAddSuccessResponse("保存通知成功!",null);
    }
    /**
     * 上位企业管理员根据房源
     * 发送通知
     * @param noticeVo
     * @param employee
     * @return
     */
    public CommonResponse sendNoticeBySystemByHouseList(NoticeVo noticeVo, Employee employee){
        noticeVo.setEnterpriseId(employee.getEnterpriseId());
        noticeVo.setOperatorId(employee.getId());
        noticeVo.setOpTime(new Date());
        noticeVo.setType(NoticeCons.TYPE_SYS);
        noticeMapper.batchInsertNotice(noticeVo);
        return CommonResponse.buildAddSuccessResponse("保存通知成功!",null);
    }
}
